package top.jandmla.excel.util.excelReadImpl;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import org.apache.commons.io.FileUtils;
import top.jandmla.excel.bean.Test;
import top.jandmla.excel.util.ExcelRead;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class Recover implements ExcelRead {

    /**
     * 文件数据读取
     *
     * @param file   上传文件对 象
     * @param nameid 上传文件ID
     * @return 文件数据列表
     */
    public List<Test> getTestFromSheet(File file, String nameid, String minId) {
        Workbook book = null;
        Sheet sheet;
        Cell name, age, phone, email;

        try {
            //hello.xls为要读取的excel文件名
            book = Workbook.getWorkbook(file);
            int number = book.getNumberOfSheets();
            //获得第一个工作表对象(ecxel中sheet的编号从0开始,0,1,2,3,....)
            sheet = book.getSheet(0);
//            获取总行数
            int Rows = sheet.getRows();
//            获取总列数
            int Cols = sheet.getColumns();
            List<Test> list = new ArrayList<>(Rows);

            for (int i = 1; i < Rows; i++) {
//                String id = minId+i+"";
                name = sheet.getCell(0, i);
                age = sheet.getCell(1, i);
                phone = sheet.getCell(2, i);
                email = sheet.getCell(3, i);
                Test test = new Test(String.valueOf(name.getContents()), Integer.valueOf(age.getContents()), String.valueOf(phone.getContents()), String.valueOf(email.getContents()), nameid);
                list.add(test);
            }
            return list;

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (book != null)
                book.close();
            if (file.exists())
                FileUtils.deleteQuietly(file);
        }
        return null;
    }
}
